﻿unit UkbmMWBinaryStreamFormatPool;

interface

uses
  Classes, Windows, SysUtils, kbmMWBinaryStreamFormat, forms;

type
  TkbmMWBinaryStreamFormatPool = class(TObject)
  private
    FObjList:TThreadList;
    FTimeout: Integer; //单位：毫秒
    FMaxCount: Integer;
    FSemaphore: Cardinal;
    function CreateNewInstance(List:TList): TkbmMWBinaryStreamFormat;
    function GetLock(List:TList;Index: Integer): Boolean;
  public
    property Timeout:Integer read FTimeout write FTimeout;
    property MaxCount:Integer read FMaxCount;

    constructor Create(ACapicity:Integer);overload;
    destructor Destroy;override;
    function Lock: TkbmMWBinaryStreamFormat;
    procedure UnLock(var Value: TkbmMWBinaryStreamFormat);
  end;

var
  kbmMWBinaryStreamFormatPool: TkbmMWBinaryStreamFormatPool;

implementation

constructor TkbmMWBinaryStreamFormatPool.Create(ACapicity:Integer);
begin
  FObjList:=TThreadList.Create;
  FTimeout := 3000;
  FMaxCount := ACapicity;
  FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);
end;

function TkbmMWBinaryStreamFormatPool.CreateNewInstance(List:TList): TkbmMWBinaryStreamFormat;
var
  p: TkbmMWBinaryStreamFormat;
begin
  try
    p := TkbmMWBinaryStreamFormat.Create(nil);
    p.Tag := 1;
    List.Add(p);
    Result := p;
  except
    Result := nil;
    Exit;
  end;
end;

destructor TkbmMWBinaryStreamFormatPool.Destroy;
var
  i: Integer;
  List:TList;
begin
  List:=FObjList.LockList;
  try
    for i := List.Count - 1 downto 0 do
    begin
      TkbmMWBinaryStreamFormat(List[i]).Free;
    end;
  finally
    FObjList.UnlockList;
  end;
  FObjList.Free;
  FObjList := nil;
  CloseHandle(FSemaphore);
  inherited Destroy;
end;

function TkbmMWBinaryStreamFormatPool.GetLock(List:TList;Index: Integer): Boolean;
begin
  try
    Result := TkbmMWBinaryStreamFormat(List[Index]).Tag = 0;
    if Result then
      TkbmMWBinaryStreamFormat(List[Index]).Tag := 1;
  except
    Result :=False;
    Exit;
  end;
end;

function TkbmMWBinaryStreamFormatPool.Lock: TkbmMWBinaryStreamFormat;
var
  i: Integer;
  List:TList;
begin
  try
    Result := nil;
    if WaitForSingleObject(FSemaphore, Timeout) = WAIT_FAILED then Exit;
    List:=FObjList.LockList;
    try
      for i := 0 to List.Count - 1 do
      begin
        if GetLock(List,i) then
        begin
          Result := TkbmMWBinaryStreamFormat(List[i]);
          //PostMessage(Application.MainForm.Handle, 8888,23,0);
          Exit;
        end;
      end;
      if List.Count < MaxCount then
      begin
        Result := CreateNewInstance(List);
        //PostMessage(Application.MainForm.Handle, 8888,21,0);
      end;
    finally
      FObjList.UnlockList;
    end;
  except
    Result :=nil;
    Exit;
  end;
end;

procedure TkbmMWBinaryStreamFormatPool.Unlock(var Value: TkbmMWBinaryStreamFormat);
var
  List:TList;
begin
  try
    List:=FObjList.LockList;
    try
      TkbmMWBinaryStreamFormat(List[List.IndexOf(Value)]).Tag :=0;
      ReleaseSemaphore(FSemaphore, 1, nil);
    finally
      FObjList.UnlockList;
    end;
    //PostMessage(Application.MainForm.Handle, 8888, 22, 0);
  except
    Exit;
  end;
end;

initialization
  kbmMWBinaryStreamFormatPool := TkbmMWBinaryStreamFormatPool.Create(100);
finalization
  FreeAndNil(kbmMWBinaryStreamFormatPool);

end.


